package com.mk.community.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

/**
 * @author 孔德成
 * @slogan 致敬大师，致敬未来的你
 * @date 2021/9/28 2:41 上午
 * @desc 在Spring中，处理外部值的最简单方式就是声明属性源并通过Spring的Environment来检索属性。
 */
@Configuration
@PropertySource("classpath:mail.properties")
public class MailConfig {

    @Autowired
    private Environment env;

    @Primary
    @Bean
    public MailProperties mailProperties() {
        MailProperties mailProperties = new MailProperties();
        mailProperties.setProtocol(env.getProperty("spring.mail.protocol"));
        mailProperties.setHost(env.getProperty("spring.mail.host"));
        mailProperties.setPort(env.getProperty("spring.mail.port", Integer.class));
        mailProperties.setPassword(env.getProperty("spring.mail.password"));
        return mailProperties;
    }
}
